package com.colin.library.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.TextView;

import com.colin.library.view.help.MetricsHelp;

import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

/**
 * 仿京东 垂直滚动
 */

@SuppressLint("AppCompatCustomView")
public class AutoTextView extends TextView {
	private Context context ;
	private int mDuration; //文字从出现到显示消失的时间
	private int mInterval; //文字停留在中间的时长切换的间隔
	private List<AutoEntry> mTexts; //显示文字的数据源
	private int mY = 0; //文字的Y坐标
	private int mIndex = 0; //当前的数据下标
	private Paint mPaintEnd; //绘制内容的画笔
	private Paint mPaintStart; //绘制前缀的画笔
	private boolean isMove = true; //文字是否移动
	private boolean hasInit = false;


	public interface OnClickScrollContentListener {
		void onClick(AutoEntry autoEntry );
	}

	private OnClickScrollContentListener mOnClickScrollContentListener;

	public void setOnClickTextContentLitener(AutoTextView.OnClickScrollContentListener mOnClickScrollContentListener) {
		this.mOnClickScrollContentListener = mOnClickScrollContentListener;
	}

	//设置数据源
	public void setTextList(List mTexts) {
		this.mTexts = mTexts;
	}

	//设置广告文字的停顿时间
	public void setInterval(int mInterval) {
		this.mInterval = mInterval;
	}

	//设置文字从出现到消失的时长
	public void setDuration(int mDuration) {
		this.mDuration = mDuration;
	}

	//设置前缀的文字颜色
	public void setFrontColor(int mFrontColor) {
		mPaintStart.setColor(mFrontColor);
	}

	//设置正文内容的颜色
	public void setBackColor(int mBackColor) {
		mPaintEnd.setColor(mBackColor);
	}


	@Override
	public boolean onTouchEvent(MotionEvent event) {
		int action = event.getAction();

		switch (action) {
			case MotionEvent.ACTION_DOWN:
				if (mOnClickScrollContentListener != null) {
					mOnClickScrollContentListener.onClick(mTexts.get(mIndex));
				}

				break;
		}
		return true;
	}


	public AutoTextView(Context context) {
		this(context, null, 0);
	}
	public AutoTextView(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
	}
	public AutoTextView(Context context, AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);
		this.context = context ;
		initData();
	}

	/**
	 * 初始化数据
	 */
	private void initData() {
		mDuration = 500;
		mInterval = 1000;
		mIndex = 0;
		mPaintStart = new Paint();
		mPaintStart.setAntiAlias(true);
		mPaintStart.setColor(getResources().getColor(R.color.gplus_color_2));
		mPaintStart.setDither(true);
		mPaintStart.setTextSize(MetricsHelp.sp2px(context,14));

		mPaintEnd = new Paint();
		mPaintEnd.setAntiAlias(true);
		mPaintEnd.setDither(true);
		mPaintEnd.setColor(getResources().getColor(R.color.gplus_color_4));
		mPaintEnd.setTextSize(MetricsHelp.sp2px(context,16));
	}


	@Override
	protected void onSizeChanged(int w, int h, int oldw, int oldh) {
		super.onSizeChanged(w, h, oldw, oldh);
	}


	@Override
	protected void onDraw(Canvas canvas) {
		if (mTexts != null) {
			AutoEntry model = mTexts.get(mIndex);
			String font = model.getStart();
			String back = model.getEnd();
			//绘制前缀
			Rect indexBound = new Rect();
			mPaintStart.getTextBounds(font, 0, font.length(), indexBound);

			//绘制内容文字
			Rect contentBound = new Rect();
			mPaintEnd.getTextBounds(back, 0, back.length(), contentBound);
			if (mY == 0 && hasInit == false) {
				mY = getMeasuredHeight() - indexBound.top;
				hasInit = true;
			}
			//移动到最上面
			if (mY == 0 - indexBound.bottom) {
				mY = getMeasuredHeight() - indexBound.top;
				mIndex++;
			}
			canvas.drawText(back, 0, back.length(), (indexBound.right - indexBound.left) + 20, mY, mPaintEnd);
			canvas.drawText(font, 0, font.length(), 10, mY, mPaintStart);
			//移动到中间
			if (mY == getMeasuredHeight() / 2 - (indexBound.top + indexBound.bottom) / 2) {
				isMove = false;
				Timer timer = new Timer();
				timer.schedule(new TimerTask() {
					@Override
					public void run() {
						postInvalidate();
						isMove = true;
					}
				}, mInterval);
			}
			mY -= 1;
			//循环使用数据
			if (mIndex == mTexts.size()) {
				mIndex = 0;
			}
			//如果是处于移动状态时的,则延迟绘制
			//计算公式为一个比例,一个时间间隔移动组件高度,则多少毫秒来移动1像素
			if (isMove) {
				postInvalidateDelayed(mDuration / getMeasuredHeight());
			}
		}

	}







	public static class AutoEntry {
		private String mStart; //前面的文字
		private String mEnd; //后面的文字
		private String mUrl ;//包含的链接

		public AutoEntry(String mStart, String mEnd, String mUrl) {
			this.mStart = mStart;
			this.mEnd = mEnd;
			this.mUrl = mUrl;
		}
		public String getStart() {
			return mStart;
		}

		public void setStart(String mStart) {
			this.mStart = mStart;
		}

		public String getEnd() {
			return mEnd;
		}

		public void setEnd(String mEnd) {
			this.mEnd = mEnd;
		}

		public String getUrl() {
			return mUrl;
		}

		public void setUrl(String mUrl) {
			this.mUrl = mUrl;
		}

		@Override
		public String toString() {
			return "AutoEntry{" +
					"mStart='" + mStart + '\'' +
					", mEnd='" + mEnd + '\'' +
					", mUrl='" + mUrl + '\'' +
					'}';
		}
	}

}
